Usage Example
Learn how to interact with your dApp using the useSessionKeyManager and SessionWalletProvider through various practical examples
Now that you have set up the hooks and provider, let’s look at some examples of using the provided methods.
Creating a Session
To create a session, call the createSession
method from the sessionWallet
. This method accepts three parameters:
targetProgramPublicKey
: APublicKey
instance representing the target program you want to interact with.topUp
: A boolean value, set totrue
if you want to top up an session keypair with0.01 SOL
initially and `false` if you dont want to topUp the session Keypair.expiryInMinutes
: An optional parameter, representing the session’s expiry time in minutes. The default value is 60 minutes.
By calling createSession
, a new ephemeral keypair is generated and stored on the client-side. The session token is then created and stored alongside the keypair. This enables the user to securely sign transactions using the generated keypair without revealing their actual wallet’s private key.
Signing and Sending a Transaction
To sign and send a transaction, use the signAndSendTransaction
method. This method first signs the transaction using the ephemeral key pair created during the session. Then, it sends the signed transaction to the Solana network.
The signAndSendTransaction
method provides an extra layer of security by ensuring that the actual wallet’s private key is not exposed. The ephemeral key pair stored on the client-side is used to sign the transaction, thus keeping the user’s main wallet secure.
Revoking a Session
To revoke a session, call the revokeSession
method from the sessionWallet
. This method performs two actions:
- It removes the ephemeral key pair and the session token from the client-side storage.
- It revokes the session from the contract.
Revoking a session ensures that the ephemeral key pair is no longer valid and usable.
These examples should help you get started with implementing session management and wallet functionality in your app.
Please refer to the “Create Post” section in the Example App to see how the Session Token implementation is done.
Was this page helpful?